Skip to content

Support for Terraform Search (.tfquery.hcl) files #2007

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 18 commits into
base: main
Choose a base branch
from

Conversation

anubhav-goel
Copy link
Contributor

@anubhav-goel anubhav-goel commented Jul 29, 2025

Description

This provides completion, hover, and diagnostics for Terraform Search files. Adds new language id - terraform-search.

Related PRs:

Demo

Note

Around 2000 lines of code are related to test cases

TODO

  • Release terraform-json and use respective version instead of pseudo-version
  • Release terraform-schema and use respective version instead of pseudo-version

@anubhav-goel anubhav-goel force-pushed the feature/tfsearch-TF-26847 branch from ea24c6b to e87b916 Compare August 6, 2025 05:48
@anubhav-goel anubhav-goel changed the title Feature/tfsearch tf 26847 Support for Terraform Search (.tfquery.hcl) files Aug 6, 2025
@anubhav-goel anubhav-goel marked this pull request as ready for review August 11, 2025 05:58
@anubhav-goel anubhav-goel requested a review from a team as a code owner August 11, 2025 05:58
Copy link
Contributor Author

@anubhav-goel anubhav-goel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM ✅

@@ -46,7 +47,7 @@ This allows IntelliSense to remain accurate e.g. when switching branches in VCS
or when there are any other changes made to these files outside the editor.

If the client implements file watcher, it should watch for any changes
in `**/*.tf`, `**/*.tfvars`, `*.tfstack.hcl`, `**/*.tfstack.hcl` and `**/*.tfstack.hcl` files in the workspace.
in `**/*.tf`, `**/*.tfvars`, `**.tfstack.hcl`, `**/*.tfcomponent.hcl`, `**/*.tfdeploy.hcl` and `**/*.tfquery.hcl` files in the workspace.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
in `**/*.tf`, `**/*.tfvars`, `**.tfstack.hcl`, `**/*.tfcomponent.hcl`, `**/*.tfdeploy.hcl` and `**/*.tfquery.hcl` files in the workspace.
in `**/*.tf`, `**/*.tfvars`, `**/*.tfstack.hcl`, `**/*.tfcomponent.hcl`, `**/*.tfdeploy.hcl` and `**/*.tfquery.hcl` files in the workspace.

_, err := f.stateStore.JobStore.EnqueueJob(ctx, job.Job{
Dir: dir,
Func: func(ctx context.Context) error {
return jobs.SchemaSearchValidation(ctx, f.store, f.moduleFeature, f.rootFeature, dir.Path())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason you did not implement ReferenceValidation?

f.logger.Printf("loading module metadata returned error: %s", jobErr)
}

// Reference collection jobs will depend on this one, so we move it here in advance
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In stacks and modules we have a decode block here that resolves component or module calls, so you get autocompletion for those. Is there not something similiar for tfquery files?

}

func searchPathContext(record *state.SearchRecord, stateReader CombinedReader) (*decoder.PathContext, error) {
resolvedVersion := tfschema.ResolveVersion(stateReader.TerraformVersion(record.Path()), record.Meta.CoreRequirements)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you need to have a job to parse terraform version in search for this to work? Or does this pull this from moduleReader?

// ParseSearchConfiguration parses the whole Search configuration,
// i.e. turns bytes of `*.tfquery.hcl` files into AST ([*hcl.File]).
func ParseSearchConfiguration(ctx context.Context, fs ReadOnlyFS, searchStore *state.SearchStore, searchPath string) error {
record, err := searchStore.SearchRecordByPath(searchPath)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The use of SearchRecordByPath makes me wonder if this should have all been Query, but I guess it's one use case so it being confusing (sounding like "search all records by path" instead of "get SearchRecords by path")

Comment on lines +41 to +46
isMatchingLanguageId := (rpcContext.LanguageID == lsp.Search.String())

// Only parse the file that's being changed/opened, unless this is 1st-time parsing
if record.DiagnosticsState[globalAst.HCLParsingSource] == operation.OpStateLoaded &&
rpcContext.IsDidChangeRequest() &&
isMatchingLanguageId {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
isMatchingLanguageId := (rpcContext.LanguageID == lsp.Search.String())
// Only parse the file that's being changed/opened, unless this is 1st-time parsing
if record.DiagnosticsState[globalAst.HCLParsingSource] == operation.OpStateLoaded &&
rpcContext.IsDidChangeRequest() &&
isMatchingLanguageId {
// Only parse the file that's being changed/opened, unless this is 1st-time parsing
if record.DiagnosticsState[globalAst.HCLParsingSource] == operation.OpStateLoaded &&
rpcContext.IsDidChangeRequest() &&
rpcContext.LanguageID == lsp.Search.String() {

This was only done because stacks has multiple files. I don't think Search has more than one?

Comment on lines +63 to +64
targets := make(reference.Targets, 0)
targets = append(targets, searchTargets...)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are there "built-in" references to consider like Stacks has?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants